package com.igl.talk.android.client.ide.emoticons;/*
 * @author Adam Wieczorek 
 *
 * 26/04/2014
 *
 * All rights reserved.
 */

import android.content.Context;
import android.text.Spannable;
import android.text.style.ImageSpan;
import com.igl.talk.android.client.R;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmoticonsBuilder {

    private static final Spannable.Factory spannableFactory = Spannable.Factory
            .getInstance();

    private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();
    static {
        addPattern(emoticons, ":)", R.drawable.emo_im_happy);
        addPattern(emoticons, ":(", R.drawable.emo_im_sad);
        addPattern(emoticons, ":D", R.drawable.emo_im_laughing);
        addPattern(emoticons, ":'(", R.drawable.emo_im_crying);
        addPattern(emoticons, ":/", R.drawable.emo_im_undecided);
        addPattern(emoticons, ":[", R.drawable.emo_im_embarrassed);
        addPattern(emoticons, "O:)", R.drawable.emo_im_angel);
        addPattern(emoticons, ":!", R.drawable.emo_im_foot_in_mouth);
        addPattern(emoticons, ":$", R.drawable.emo_im_money_mouth);
        addPattern(emoticons, "B)", R.drawable.emo_im_cool);
        addPattern(emoticons, ":", R.drawable.emo_im_kissing);
        addPattern(emoticons, ":O", R.drawable.emo_im_yelling);
        addPattern(emoticons, "=O", R.drawable.emo_im_surprised);
        addPattern(emoticons, ":P", R.drawable.emo_im_tongue_sticking_out);
        addPattern(emoticons, ";)", R.drawable.emo_im_winking);
        addPattern(emoticons, ":X", R.drawable.emo_im_lips_are_sealed);
        addPattern(emoticons, "o.O", R.drawable.emo_im_wtf);
    };

    private static void addPattern(Map<Pattern, Integer> map, String smile,
                                   int resource) {
        map.put(Pattern.compile(Pattern.quote(smile)), resource);
    }

    public static boolean insertEmoticons(Context context, Spannable spannable) {
        boolean hasChanges = false;
        for (Map.Entry<Pattern, Integer> entry : emoticons.entrySet()) {
            Matcher matcher = entry.getKey().matcher(spannable);
            while (matcher.find()) {
                boolean set = true;
                for (ImageSpan span : spannable.getSpans(matcher.start(),
                        matcher.end(), ImageSpan.class))
                    if (spannable.getSpanStart(span) >= matcher.start()
                            && spannable.getSpanEnd(span) <= matcher.end())
                        spannable.removeSpan(span);
                    else {
                        set = false;
                        break;
                    }
                if (set) {
                    hasChanges = true;
                    spannable.setSpan(new ImageSpan(context, entry.getValue()),
                            matcher.start(), matcher.end(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return hasChanges;
    }

    public static Spannable buildEmoticonsMessage(Context context, CharSequence text) {
        Spannable spannable = spannableFactory.newSpannable(text);
        insertEmoticons(context, spannable);
        return spannable;
    }
}
